import express from 'express'
import QuestionService from '../../services/QuestionService'
import ResponseHelper from '../ResponseHelper';

const router = express.Router();

/**
 * 添加一个题目
 */
router.post('/', async function (req, res, next) {
  const body = req.body

  try {
    const result = await QuestionService.create(body)

    ResponseHelper.send(result, res)
  } catch (err) {
    next()
  }

})

router.delete('/:id', async function (req, res, next) {
  const id = req.params.id
  try {
    const result = await QuestionService.delete(id)
    ResponseHelper.send(result, res)
  } catch {
    next()
  }

})

router.get('/findByPage', async (req, res, next) => {
  const page = req.query.page || 1
  const limit = req.query.limit || 10

  try {
    const result = await QuestionService.findByPage(+page, +limit)
    ResponseHelper.send(result, res)
  } catch (err) {
    next()
  }
})

export default router